<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>列出 MySQL 结果中的字段</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mysql-list-dbs.html">« mysql_list_dbs</a></li>
      <li style="float: right;"><a href="function.mysql-list-processes.html">mysql_list_processes »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mysql.html">MySQL 函数</a></li>
    <li>列出 MySQL 结果中的字段</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mysql-list-fields" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_list_fields</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_list_fields</span> &mdash; <span class="dc-title">列出 MySQL 结果中的字段</span></p>

   </div>
   <div class="refsect1 unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-seealsw" id="refsect1-function.mysql-list-fields-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-seealsw">
    <h3 class="title">说明</h3>
    <div class="methodsynopsis dc-description">
     <span class="methodname"><strong>mysql_list_fields</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$database_name</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$table_name</code></span>, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$link_identifier</code><span class="initializer"> = ?</span></span>): <span class="type">resource</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>mysql_list_fields()</strong></span>
     取得给定表名的信息。参数是数据库名和表名。返回一个结果指针，可以用于
     <span class="function"><a href="function.mysql-field-flags.html" class="function">mysql_field_flags()</a></span>，<span class="function"><a href="function.mysql-field-len.html" class="function">mysql_field_len()</a></span>，<span class="function"><a href="function.mysql-field-name.html" class="function">mysql_field_name()</a></span> 和
     <span class="function"><a href="function.mysql-field-type.html" class="function">mysql_field_type()</a></span>。
    </p>
    <p class="para">
     <div class="example" id="example-1771">
      <p><strong>示例 #1 <span class="function"><strong>mysql_list_fields()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mysql_password'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$fields&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_list_fields</span><span style="color: #007700">(</span><span style="color: #DD0000">"database1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"table1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$columns&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_num_fields</span><span style="color: #007700">(</span><span style="color: #0000BB">$fields</span><span style="color: #007700">);<br /><br />for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$columns</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">mysql_field_name</span><span style="color: #007700">(</span><span style="color: #0000BB">$fields</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}</span>
</span>
</code></div>
      </div>

      <div class="example-contents"><p>
       以上例子将产生如下输出：
      </p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
field1
field2
field3
...
</pre></div>
      </div>
     </div>
    </p>
    <p class="para">
     为向下兼容仍然可以使用
     <span class="function"><strong>mysql_listfields()</strong></span>，但反对这样做。
    </p>
    <blockquote class="note"><p><strong class="note">注意</strong>: 
     <p class="para">
      <span class="function"><strong>mysql_list_fields()</strong></span> 函数已过时。最好用
      <span class="function"><a href="function.mysql-query.html" class="function">mysql_query()</a></span> 来发出一条
      <code class="literal">SHOW COLUMNS FROM table [LIKE &#039;name&#039;]</code>
      的 SQL 语句来代替。
     </p>
    </p></blockquote>
   </div>

  </div></div></div></body></html>